<style>
.Wdate{
	height:30px;
	width:250px;
}
</style>
<div class="row">
	<div class="col-lg-12">
		<h1 class="page-header">ip白名单</h1>
	</div>
	<!-- /.col-lg-12 -->
</div>
<div class="row">
	<?php
	$ipLimit = (new LoginServerConfig)->getValueByKey('ip_limit_config_global');
	if($ipLimit) {
		$ipLimit = json_decode($ipLimit, true);
		$ips = implode(";", $ipLimit['ips']);
	} else {
		$ips = '';
	}
	?>
</div>
<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="form-group">
					<label>ip白名单-全局</label>
					<textarea class="form-control" rows="3" placeholder="输入ip，英文分号分隔" id="ips1"><?=$ips?></textarea>
				</div>
				<button type="button" class="btn btn-primary" onclick="save1()">保存</button>
			</div>

			<?php
			$ipLimitSingle = (new LoginServerConfig)->getValueByKey('ip_limit_config_single');
			if($ipLimitSingle) {
				$ipLimitSingle = json_decode($ipLimitSingle, true);
			} else {
				$ipLimitSingle = [];
			}

			?>

			<div class="panel-body">
				<div class="form-group" id="block_usercode">
				</div>
				<div class="form-group">
					<label>ip白名单</label>
					<br>
					<select id="singleIp" onchange="setIps();">
						<option value ="0">选择一个服务器</option>
						<?php foreach ($serverList as $v): ?>
						<option value ="<?=$v['id']?>"><?=$v['name']?></option>
						<?php endforeach;?>
					</select>
					<br>
					<textarea class="form-control" rows="3" placeholder="输入ip，英文分号分隔" id="ips2"></textarea>
				</div>
				<button type="button" class="btn btn-primary" onclick="save2()">保存</button>
			</div>


		</div>
	</div>
</div>

<script>
	var ipLimit = <?=json_encode(Set::combine($ipLimitSingle, '{n}.serverId', '{n}.ips'))?>;
	function save1(){
		var ips = $.trim($("#ips1").val());
//		if(!ips) {
//			alert('不能为空');
//			return;
//		}
		$.post('/admin/ajaxIpLimit/1', {"ips":ips}, function(o){
            alertOk('更改成功', flushPage);
		});
	}
	function save2(){
		var serverId = $.trim($('#singleIp').val());
		var ips = $.trim($("#ips2").val());
//		if(!serverId || !ips)  {
//			alert('不能为空');
//			return;
//		}
		$.post('/admin/ajaxIpLimit/2', {"serverId":serverId,"ips":ips}, function(o){
            alertOk('更改成功', flushPage);
		});
	}
	function setIps(){
		var serverId = $.trim($('#singleIp').val());
		if(ipLimit && ipLimit[serverId]) {
			$('#ips2').val(ipLimit[serverId].join(';'));
		} else {
			$('#ips2').val('');
		}
		$('#ips2').focus();
	}
    function flushPage(){
        location.reload();
    }
</script>